﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Com
{
    class DataLogs
    {
        bool m_debug = false;
        string m_openFile = "";
        FileStream fs = null;
        StreamWriter outputFile = null;

        public DataLogs()
        {

        }
        ~DataLogs()
        {
            //Close();
        }

        public bool CreateDir(string dir = "logs")
        {
            if (dir == "")
                return false;

            string fullDir = Directory.GetCurrentDirectory() + "\\" + dir;
            if (!System.IO.Directory.Exists(fullDir))
            {
                System.IO.Directory.CreateDirectory(fullDir);
            }
            return true;
        }
        public bool FileExists(string file = "")
        {
            if (file == "")
            {
                file = Directory.GetCurrentDirectory() + "\\" + "logs" + "\\" +
                "log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            }
            return System.IO.File.Exists(file);
        }
        public void Create(string file = "")
        {
            if (file == "")
                file = Directory.GetCurrentDirectory() + "\\" + "logs" + "\\" +
                "log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

        }
        public void Open(string file = "")
        {
            if (file == "")
                file = Directory.GetCurrentDirectory() + "\\" + "logs" + "\\" +
                "log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            if (fs != null && outputFile != null)
            {
                Close();
            }
            m_openFile = file;

            if (System.IO.File.Exists(file))
            {
                // 打开文件
                fs = new FileStream(file, FileMode.Append);
            }
            else
            {
                fs = new FileStream(file, FileMode.Create);
            }
            outputFile = new StreamWriter(fs);
        }
        /// <summary>
        /// 读取指定文件中所有内容以string返回
        /// </summary>
        /// <param name="fileName">读取的文件名，如果没有文件路径则默认当前文件夹下</param>
        /// <returns>对应文件中所有内容</returns>
        public string ReadStr(string fileName)
        {
            string datas = "";
            string file;
            if (fileName == "")
                file = "logs" + "\\" +
                "log-" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ffff") + ".txt";
            else
                file = fileName;

            FileStream fs = null;

            try
            {
                if (System.IO.File.Exists(file))
                {
                    // 打开文件
                    fs = new FileStream(file, FileMode.Open);
                }
                else
                {
                    return datas;
                }
                using (StreamReader outputFile = new StreamReader(fs))
                {
                    datas = outputFile.ReadToEnd();

                    //关闭流
                    outputFile.Close();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                if (m_debug)
                    Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
            return datas;
        }
        public byte[] ReadBytes()
        {
            return null;
        }
        public bool WriteLine(byte[] datas, string head = "recv：")
        {
            if (fs == null || outputFile == null)
            {
                return false;
            }
            outputFile.Write(head);
            for (int i = 0; i < datas.Length; i++)
            {
                outputFile.Write(datas[i].ToString("X2"));
                outputFile.Write(" ");
            }
            outputFile.WriteLine("\r\n");
            //清空缓冲区
            outputFile.Flush();
            return true;
        }
        public void Close()
        {
            m_openFile = "";

            if (fs == null || outputFile == null)
            {
                return;
            }
            //关闭流
            outputFile.Close();
            fs.Close();
        }

        // 单独打开和写入，不和上面一起使用
        public void Save(byte[] datas, string head = "recv：")
        {
            string file = Directory.GetCurrentDirectory() + "\\" + "logs" +
                "log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            FileStream fs = null;

            try
            {
                if (System.IO.File.Exists(file))
                {
                    // 打开文件
                    fs = new FileStream(file, FileMode.Append);
                }
                else
                {
                    fs = new FileStream(file, FileMode.Create);
                }
                using (StreamWriter outputFile = new StreamWriter(fs))
                {
                    //if (datas.Length > 0)
                    //    outputFile.WriteLine(datas);    // WriteLine写入后自动换行
                    outputFile.Write(head);
                    for (int i = 0; i < datas.Length; i++)
                    {
                        if (datas[i] < 0x10)
                            outputFile.Write(0);
                        outputFile.Write(datas[i]);
                        outputFile.Write(" ");
                    }
                    outputFile.WriteLine("\r\n");

                    //清空缓冲区
                    outputFile.Flush();
                    //关闭流
                    outputFile.Close();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                if (m_debug)
                    Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        // 返回文件名
        public string Save(string datas, string fileName = "", FileMode fileMode = FileMode.Append)
        {
            string file;
            if (fileName == "")
                file = "logs" + "\\" +
                "log-" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ffff") + ".txt";
            else
                file = fileName;

            FileStream fs = null;

            try
            {
                if (System.IO.File.Exists(file))
                {
                    // 打开文件
                    fs = new FileStream(file, fileMode);
                }
                else
                {
                    fs = new FileStream(file, FileMode.Create);
                }
                using (StreamWriter outputFile = new StreamWriter(fs))
                {
                    //if (datas.Length > 0)
                    //    outputFile.WriteLine(datas);    // WriteLine写入后自动换行
                    outputFile.Write(datas);
                    //outputFile.WriteLine("\r\n");

                    //清空缓冲区
                    outputFile.Flush();
                    //关闭流
                    outputFile.Close();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                if (m_debug)
                    Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
            return file;
        }


        private bool CheckStreamIsOk()
        {
            return true;
        }
    }
}
